package com.lanou.renrenxin.tools;

import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.ImageMessageBody;
import com.easemob.chat.LocationMessageBody;
import com.easemob.chat.NormalFileMessageBody;
import com.easemob.chat.TextMessageBody;
import com.easemob.chat.VoiceMessageBody;
import com.lanou.renrenxin.adapter.ChattingAdapter;
import com.lanou.renrenxin.dao.ChatList;
import com.lanou.renrenxin.singleton.RenrenxinSingleton;

import java.io.File;

/**
 * Created by zouhui on 16/3/9.
 */
public class SendMessage {
    private Handler handler;
    private ChattingAdapter adapter;
    private RecyclerView recyclerView;
    private Handler sendHandler;
    private ChattingAdapter.ChattingSendImageViewHolder sendImageViewHolder;
    private EMConversation conversation;
    private String username;

    public SendMessage() {
    }

    public SendMessage(ChattingAdapter adapter, Handler handler, RecyclerView recyclerView,String username) {
        this.adapter = adapter;
        this.handler = handler;
        this.recyclerView = recyclerView;
        this.username = username;
        conversation = EMChatManager.getInstance().getConversation(username);
    }

    /**
     * 发送文本消息
     */
    public void sendTextMessage(final String sendMsgContent, final String mUsername, final String username, final String time) {
        //获取到与聊天人的会话对象。参数username为聊天人的userid或者groupid，后文中的username皆是如此
        EMConversation conversation = EMChatManager.getInstance().getConversation(username);
//创建一条文本消息
        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
//如果是群聊，设置chattype,默认是单聊
//        message.setChatType(ChatType.GroupChat);
//设置消息body
        TextMessageBody txtBody = new TextMessageBody(sendMsgContent);
        message.addBody(txtBody);
//设置接收人
        message.setReceipt(username);
//把消息加入到此会话对象中
        conversation.addMessage(message);
        ChatList chat = new ChatList();
        chat.setMUsername(mUsername);
        chat.setUsername(username);
        chat.setTime(time);
        chat.setBody(sendMsgContent);
        chat.setType(0);
        chat.setMsg_type(StringValues.MSG_TYPE_TXT);
        RenrenxinSingleton.getInstence().saveChatList(chat);
        adapter.send(message);
        recyclerView.scrollToPosition(adapter.getItemCount()-1);

    }


    /**
     * 发送图片
     *
     * @param filePath
     */
    public void sendImage(String filePath,String mUsername,String username,String time) {
        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.IMAGE);
        //如果是群聊，设置chattype,默认是单聊
        //message.setChatType(EMMessage.ChatType.GroupChat);

        final ImageMessageBody body = new ImageMessageBody(new File(filePath));
        // 默认超过100k的图片会压缩后发给对方，可以设置成发送原图
        // body.setSendOriginalImage(true);
        final ChatList chat = new ChatList();
        chat.setTime(time);
        chat.setMUsername(mUsername);
        chat.setUsername(username);
        chat.setMsg_type(StringValues.MSG_TYPE_IMAGE);
        chat.setBody("[图片]");
        chat.setType(2);
        RenrenxinSingleton.getInstence().saveChatList(chat);
        message.addBody(body);
        message.setReceipt(username);
        conversation.addMessage(message);
        adapter.send(message);
        recyclerView.scrollToPosition(adapter.getItemCount() - 1);
    }


    /**
     * 发送语音
     *
     * @param filePath
     * @param len
     */
    public void sendVoice(final String filePath, int len,String mUsername, final String username,RecorderVoice recorderVoice,String isSend) {
        final int lens[] = {len};

        if (!isSend.equals(filePath)) {
            conversation = EMChatManager.getInstance().getConversation(username);
            final EMMessage message = EMMessage.createSendMessage(EMMessage.Type.VOICE);
            //如果是群聊，设置chattype,默认是单聊
//        message.setChatType(ChatType.GroupChat);
            final ChatList chat = new ChatList();
            chat.setMsg_type(StringValues.MSG_TYPE_VOICE);
            chat.setMUsername(mUsername);
            chat.setUsername(username);
            chat.setType(2);
            chat.setVoiceLen(len);
            chat.setBody("[语音]");
            chat.setTime(FormatTime.formatTime(message.getMsgTime()));
            RenrenxinSingleton.getInstence().saveChatList(chat);
            VoiceMessageBody body = new VoiceMessageBody(new File(filePath), lens[0]);
            message.addBody(body);
            message.setReceipt(username);
            conversation.addMessage(message);
            adapter.send(message);
            recyclerView.scrollToPosition(adapter.getItemCount()-1);
        }

    }


    public void sendLocation(String locationAddress,double latitude,
                             double longitude,String username,String mUsername) {
        Log.e("SendMessage", locationAddress + latitude + longitude);

        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.LOCATION);
//如果是群聊，设置chattype,默认是单聊
//        message.setChatType(EMMessage.ChatType.GroupChat);
        LocationMessageBody locBody = new LocationMessageBody(locationAddress, latitude, longitude);
        ChatList chat = new ChatList();
        chat.setBody("[位置]"+locationAddress);
        chat.setMsg_type(StringValues.MSG_TYPE_LOCATION);
        chat.setType(2);
        chat.setMUsername(mUsername);
        chat.setUsername(username);
        RenrenxinSingleton.getInstence().saveChatList(chat);
        chat.setTime(FormatTime.formatTime(FormatTime.getSysTime()));
        message.addBody(locBody);
        message.setReceipt(username);
        conversation.addMessage(message);
        adapter.send(message);
        recyclerView.scrollToPosition(adapter.getItemCount() - 1);
    }



    public void sendFile(String filePath,String mUsername){

        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);

//        if (chatType == CHATTYPE_GROUP)
//            message.setChatType(ChatType.GroupChat);
        message.setReceipt(username);
        NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
        message.addBody(body);
        conversation.addMessage(message);
        ChatList chat = new ChatList();
        chat.setMsg_type(StringValues.MSG_TYPE_FILE);
        chat.setType(2);
        chat.setMUsername(mUsername);
        chat.setUsername(username);
        chat.setBody("[文件]"+body.getFileName());
        RenrenxinSingleton.getInstence().saveChatList(chat);
        adapter.send(message);
        recyclerView.scrollToPosition(adapter.getItemCount()-1);
    }

}
